﻿using Domain.Process_Procedures;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.ProcessCategory;
using ReturnValue;

namespace MES.Server.API.Application.Handler.ProcessCategory
{
    public class DeleteProcessCategoryHandler : IRequestHandler<DeleteProcessCategoryCommand, ApiResult<int>>
    {
        private readonly IRepository<ProcessCategoryModel> categoryRepo;

        public DeleteProcessCategoryHandler(IRepository<ProcessCategoryModel> CategoryRepo)
        {
            categoryRepo = CategoryRepo;
        }

        public async Task<ApiResult<int>> Handle(DeleteProcessCategoryCommand request, CancellationToken cancellationToken)
        {
            var result=await categoryRepo.GetValue(request.Id);
            if (result == null)
            {
                return new ApiResult<int>().Fail("工艺分类不存在");
            }
            result.IsDel = true;
            var r = await categoryRepo.UpdateAsync(result);
            if (r > 0)
            {
                return new ApiResult<int>().Success("删除成功",r);
            }else
            {
                return new ApiResult<int>().Fail("删除失败");
            }
        }
    }
}
